<div id="panel-button" class="row">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				<div class="col-sm-4 pull-left text-left">
					<strong class="customer_name">{{$customer->name}}</strong>
				</div>
				<div class="col-sm-4 pull-right text-right">
					{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
				</div>
			</div>
		</div>
	</div>
</div>

<div id="panel-button-fixed" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div class="panel-body text-right">
				<div class="col-sm-4 pull-left text-left">
					<strong class="customer_name">{{$customer->name}}</strong>
				</div>
				<div class="col-sm-4 pull-right text-right">
					{{Form::button(trans('form.back'), array('class' => 'btn btn-default', 'id' => 'cancel'))}}
				</div>
			</div>
		</div>
	</div>
</div>

<div id="panel-error" class="row hidden">
	<div class="col-md-12">
		<div class="panel panel-default">
			<div id="error-content" class="panel-body"></div>
		</div>
	</div>
</div>

<div class='row'>
	<div class='col-md-12'>
		<div class="panel panel-default">
			<header class="panel-heading">{{ trans('product.current_product') }}</header>
			<div class="panel-body">
				<div class="col-sm-4">
					{{Form::text('filter', '', array('placeholder' => trans('form.filter'), 'class' => 'form-control', 'id' => 'filter'))}}
				</div>
				<div class="col-sm-8">
					<div class="col-sm-8 text-right">
						{{Form::button(trans('form.add_all'), array("class" => 'btn btn-mini btn-success', 'id' => 'add_all'))}}
						{{Form::button(trans('form.add_by_brand'), array("class" => 'btn btn-mini btn-info', 'id' => 'add_by_brand'))}}
					</div>
					<div class="col-sm-4">
						<?php
							$brand[0] = trans('form.select');
						?>
						{{Form::select('brand', $brand, 0, array('class' => 'form-control'))}}
					</div>
				</div>
			</div>
			<div class="panel-body">
				<table class="table table-striped footable" data-filter="#filter" data-page-size="10" data-filter-minimum="1">
					<tbody>
						@foreach($products as $row)
						<tr data-product="{{$row->id}}" data-customer="{{$customer->id}}">
							<td>{{HTML::link('admin/product/edit/' . $row->id, $row->name)}}</td>
							<td>{{$row->net}}</td>
							<td>{{$row->brand}}</td>
							<td>{{$row->type}}</td>
							<td>{{Form::button(trans('form.add'), array('data-product' => $row->id, 'data-customer' => $customer->id, 'id' => 'add', 'class' => 'btn btn-success'))}}</td>
						</tr>
						@endforeach
					</tbody>
					<tfoot>
						<tr>
							<td colspan="5" class="text-right">
								<ul class="pagination pagination-sm"></ul>
							</td>
						</tr>
					</tfoot>
				</table>
			</div>
		</div>
	</div>

	<div class='col-md-12'>
		<div class="panel panel-default">
			<header class="panel-heading">{{ trans('product.selected_product') }}</header>
			<div class="panel-body">
				<div class="col-sm-4">
					
				</div>
				<div class="col-sm-8">
					<div class="col-sm-8 text-right">
						{{Form::button(trans('form.delete_all'), array("class" => 'btn btn-mini btn-warning', 'id' => 'delete_all'))}}
						{{Form::button(trans('form.delete_by_brand'), array("class" => 'btn btn-mini btn-danger', 'id' => 'delete_by_brand'))}}
					</div>
					<div class="col-sm-4">
						<?php
							$brand[0] = trans('form.select');
						?>
						{{Form::select('brand_to_delete', $brand, 0, array('class' => 'form-control'))}}
					</div>
				</div>
			</div>
			<div class="panel-body">
				{{Form::open(array('method' => 'post', 'id' => 'selected_products_form'))}}
				<table id="selected_products" class="table table-striped">
					<tbody>
						
					</tbody>
					<tfoot>
						<tr>
							<td colspan="9">
								<ul class="pagination pagination-sm"></ul>
							</td>
						</tr>
					</tfoot>
				</table>
				@if(isset($customer->id))
				{{Form::hidden('customer', $customer->id)}}
				@endif
				{{Form::close()}}
			</div>
		</div>
	</div>
</div>

<script>
$(document).ready(function(){
	function appendProduct(product){
		var html = '<tr id="' + product.id + '">';

		html += '<td>';
		html += product.name;
		html += '</td>';

		html += '<td>';
		html += product.net;
		html += '</td>';

		html += '<td>';
		html += product.brand;
		html += '</td>';

		html += '<td>';
		html += product.type;
		html += '</td>';

		html += '<td style="width: 135px;">';
		html += '<input id="price" type="text" class="form-control" value="' + product.price + '" data-id="' + product.id + '" />';
		html += '</td>';

		html += '<td>';
		html += '<span id="delete" data-id="' + product.id + '" class="typcn typcn-delete text-red"></span>';
		html += '</td>';

		html += '</tr>';

		$("table#selected_products tbody").append(html);
	}

	function loadSelectedProducts(){
		$.ajax({
			url: '{{URL::to('admin/product-customer/get')}}',
			type: 'post',
			data: {'customer': {{$customer->id}}},
			beforeSend: ajaxLoading(),
			success: function(result) {
				var rs = $.parseJSON(result);

				if(rs.error == true){
					appendErrorMessages(rs.message);
				}else{
					$("table#selected_products tbody").empty();

					$.each(rs.data, function(index, val) {
						appendProduct(val);
					});
				}

				ajaxLoadSuccess();
			}
		});
	}

	loadSelectedProducts();

	$("button#add").click(function(event) {
		/* Act on the event */
		var customer = $(this).attr('data-customer');
		var product = $(this).attr('data-product');

		$.ajax({
			url: '{{URL::to('admin/product-customer/add')}}',
			type: 'post',
			data: {'customer': customer, 'product' : product, 'price': 0},
			beforeSend: ajaxLoading(),
			success: function(result){
				var obj = $.parseJSON(result);

				if(obj.error == true){
					appendErrorMessages(obj.message);
				}else{
					$("tr[data-product='" + product + "']").addClass('hidden');

					loadSelectedProducts();
				}

				ajaxLoadSuccess();
			}
		});
		
	});

	$("button#add_by_brand").click(function(event) {
		var brand = $("select[name='brand']").val();
		var customer = {{$customer->id}};

		if(brand > 0){
			$.ajax({
				url: '{{URL::to('admin/product-customer/add-by-brand')}}',
				type: 'post',
				data: {'customer': customer, 'brand': brand},
				beforeSend: ajaxLoading(),
				success: function(result){
					var rs = $.parseJSON(result);

					if(rs.error == false){
						window.location.reload();
					}else{
						appendErrorMessages(rs.message);
					}

					ajaxLoadSuccess();
				}
			});
		}else{
			alert("Vui lòng chọn thương hiệu");
		}
	});

	$("button#add_all").click(function(event) {
		var ok = confirm("Bạn muốn thêm tất cả sản phẩm?");

		if(ok){
			var customer = {{$customer->id}};

			$.ajax({
				url: '{{URL::to('admin/product-customer/add-all')}}',
				type: 'post',
				data: {'customer': customer},
				beforeSend: ajaxLoading(),
				success: function(result){
					var rs = $.parseJSON(result);

					if(rs.error == false){
						window.location.reload();
					}else{
						appendErrorMessages(rs.message);
					}

					ajaxLoadSuccess();
				}
			});
		}
	});

	$("button#delete_by_brand").click(function(event) {
		var brand = $("select[name='brand_to_delete']").val();
		
		if(brand > 0){
			var ok = confirm("Bạn muốn xóa các sản phẩm này?");
			var customer = {{$customer->id}};

			if(ok){
				$.ajax({
					url: '{{URL::to('admin/product-customer/delete-by-brand')}}',
					type: 'post',
					data: {'customer': customer, 'brand': brand},
					beforeSend: ajaxLoading(),
					success: function(result){
						var rs = $.parseJSON(result);

						if(rs.error == false){
							window.location.reload();
						}else{
							appendErrorMessages(rs.message);
						}

						ajaxLoadSuccess();
					}
				});
			}
		}else{
			alert("Vui lòng chọn thương hiệu");
		}
	});

	$("button#delete_all").click(function(event) {
		var ok = confirm("Bạn muốn xóa tất cả sản phẩm?");

		if(ok){
			var customer = {{$customer->id}};

			if(customer > 0){
				$.ajax({
					url: '{{URL::to('admin/product-customer/delete-all')}}',
					type: 'post',
					data: {'customer': customer},
					beforeSend: ajaxLoading(),
					success: function(result){
						var rs = $.parseJSON(result);

						if(rs.error == false){
							window.location.reload();
						}else{
							appendErrorMessages(rs.message);
						}

						ajaxLoadSuccess();
					}
				});
			}else{
				alert("Vui lòng chọn thương hiệu");
			}
		}
	});

	$("body").on('click', 'span#delete', function(event) {
		var id = $(this).attr('data-id');

		$.ajax({
			url: '{{URL::to('admin/product-customer/delete')}}',
			type: 'post',
			data: {'id': id},
			beforeSend: ajaxLoading(),
			success: function(result){
				var rs = $.parseJSON(result);

				if(rs.error == true){
					appendErrorMessages(rs.message);
				}else{
					window.location.reload();
				}

				ajaxLoadSuccess();
			}
		});
		
	});

	$("body").on('change', 'input#price', function(event) {
		var price = $(this).val();
		var id = $(this).attr('data-id');

		if(price == 0 || isNaN(price) || price.length == 0 || price == null || price < 0){
			price = 0;
			$(this).val(0);
		}

		deleteErrorMessages();

		$.ajax({
			url: '{{URL::to('admin/product-customer/change-price')}}',
			type: 'post',
			data: {'id': id, 'price': price},
			beforeSend: ajaxLoading(),
			success: function(result){
				var rs = $.parseJSON(result);

				if(rs.error == true){
					appendErrorMessages(rs.message);
				}

				ajaxLoadSuccess();
			}
		});
	});

	$("button#cancel").click(function(){
		var url = "{{URL::to('admin/' . $customer->type)}}";
		window.location = url;
	});
});
</script>
<style type="text/css">
tr#new_selected_product{
	cursor: pointer;
}

tr:hover > td, tr:hover > td a{
	font-weight: normal !important;
}

span#delete{
	font-size: 16pt;
}
</style>